package android.Spider;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FriendSearch extends Activity {

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		
		m_connection.Initialize(this);

		setContentView(R.layout.friendsearch);

		m_prefereces = SpiderPreferences.Instance(this);
		m_usrProfile = new ProfileUser();
		
		// Need to wait for profiles.
		if (m_prefereces.m_lstAllProfiles == null)
		{
			// Need to wait for preferences for some time.
			m_dlgProgress = ProgressDialog.show(this, "Loading profiles", "Please wait while loading...", true, true);
		}
		else
		{
			// We already have all profiles.
			SetAutocompleteAdapter();
		}
	}

	private String ExtractGMail(String strText)
	{
		String strResult = "";
		int nBegin = strText.indexOf("<");
		int nEnd = strText.indexOf(">");
		if (nBegin < nEnd)
			strResult = strText.substring(nBegin + 1, nEnd);
		else
			strResult = strText;
		
		return strResult;
	}
	
	private void SetAutocompleteAdapter()
	{
		ArrayList<String> lstNames = new ArrayList<String>();
		for (int i = 0; i < m_prefereces.m_lstAllProfiles.size(); ++i)
		{
			ProfileUser profile = m_prefereces.m_lstAllProfiles.get(i);
			lstNames.add(profile.m_strFirstName + " " + profile.m_strLastName + " <" + profile.m_strGMail + ">");
		}
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.simple_text_view, lstNames);
		AutoCompleteTextView autoText = (AutoCompleteTextView)findViewById(R.id.FriendSearchACTV);
		autoText.setAdapter(adapter);
		autoText.setOnItemSelectedListener(
				new AdapterView.OnItemSelectedListener() {

					@SuppressWarnings("unchecked")
					public void onItemSelected(AdapterView parent, View v, int position, long id)
					{
						String str = ExtractGMail(((TextView)v).getText().toString());
						m_usrProfile = SpiderPreferences.Instance(FriendSearch.this).FindProfile(str);
						SetProfileCard();
					}

					@SuppressWarnings("unchecked")
					public void onNothingSelected(AdapterView arg0)
					{
						m_usrProfile = new ProfileUser();
						SetProfileCard();
					}
					
				}
			);
		autoText.setOnItemClickListener(
				new AdapterView.OnItemClickListener() {
						@SuppressWarnings("unchecked")
						public void onItemClick(AdapterView parent, View v, int position, long id)
						{
							String str = ExtractGMail(((TextView)v).getText().toString());
							m_usrProfile = SpiderPreferences.Instance(FriendSearch.this).FindProfile(str);
							SetProfileCard();
						}
					}
				);
		
	}
	
	@Override
	protected void onDestroy() {
		m_connection.Unregister();
		super.onDestroy();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    super.onCreateOptionsMenu(menu);
	    
	    menu.add(0, 0, "Send private message", R.drawable.submitsend);
	    menu.add(0, 1, "Add to contacts", R.drawable.addcontact);
	    menu.add(0, 2, "Discard", R.drawable.discard);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item)
	{
	    switch (item.getId())
	    {
	    case 0:
	    	Intent intent = new Intent(this, PrivateMessageSend.class);
	    	intent.putExtra(ThreadDisplay.s_strUser, m_usrProfile.m_strGMail);
	    	startSubActivity(intent, 0);
	    	break;
	    case 1:
	    	Toast.makeText(this, "Disabled at the moment. Sorry for inconenience.", Toast.LENGTH_SHORT).show();
	    	break;
	    case 2:
    		finish();
    		break;
	    default:
	    	return false;
	    }
	    
	    return true;
	}

	void SetProfileCard()
	{
		ImageView img = (ImageView)findViewById(R.id.AvatarOfFoundProfile);
		img.setImageResource(ProfileUser.idImages[m_usrProfile.m_nAvatar]);
		
		TextView tvName = (TextView)findViewById(R.id.NameOfFoundProfile);
		tvName.setText(m_usrProfile.m_strFirstName + " " + m_usrProfile.m_strLastName);
		
		TextView tvFrom = (TextView)findViewById(R.id.FromOfFoundProfile);
		tvFrom.setText(ProfileUser.COUNTRIES[m_usrProfile.m_nCountry] + ", " + m_usrProfile.m_strTown);
		
		String tempStr = "";

		String arrName[] = {
				"Entertainment",
				"Sport & Health",
				"Fashion & Shopping",
				"Technology",
				"Celebrity",
				"Politics",
				"Miscelenious"
		};
		
		for(int i=0;i<ProfileUser.arrInterestsFlags.length; ++i)
		{
			if ((m_usrProfile.m_nInterests & ProfileUser.arrInterestsFlags[i]) != 0)
			{
				if (tempStr.length() > 0)
					tempStr = tempStr + ", ";

				tempStr = tempStr + arrName[i];
			}
		}
		
		TextView tvInterests = (TextView)findViewById(R.id.InterestsOfFoundProfile);
		tvInterests.setText(tempStr);
		
		final String arrOccupation[] = {
				"Computers",
				"Design",
				"Education",
				"Health",
				"Industry",
				"Services",
				"Other"
		};
		
		TextView tvOccupation = (TextView)findViewById(R.id.OccupationOfFoundProfile);
		tvOccupation.setText(arrOccupation[m_usrProfile.m_nOccupation == ProfileUser.Other ? 6 : m_usrProfile.m_nOccupation]);
	}
	
	ProfileUser m_usrProfile;
	SpiderPreferences m_prefereces;
	XmppConnection m_connection = new XmppConnection() {

		@Override
		protected void OnConnected() { }

		@Override
		protected void OnProfilesReceived(ArrayList<ProfileUser> lstProfiles)
		{
			if (m_dlgProgress != null)
			{
				m_dlgProgress.cancel();
				m_dlgProgress = null;
			}
			
			SetAutocompleteAdapter();
		}
		
	};

   ProgressDialog m_dlgProgress = null;
}
